<template>
  <div>
    <el-form>
    <!-- <el-form-item label="姓名" prop="name">
            <el-input v-model="name"></el-input>
        </el-form-item> -->
    <el-upload
      class="avatar-uploader"
      action="/page/set"
      :data="Imgdata"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :on-change="handleChange"
      :file-list="fileList">
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
      <el-button  type="success" @click="shangchuan">点击上传</el-button>

    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
       fileList: [],
       Imgdata:{
         view:'用户管理',
          code:'wzwh'
       },
      Uploadurl: '/page/set', // 图片上传地址
      imageUrl:'',
    }
  },

  created () {

  },

  methods: {
    handleAvatarSuccess(res, file,fileList) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(res, file,fileList);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    //通过onchanne触发方法获得文件列表
         handleChange(file, fileList) {
            this.fileList = fileList;
            console.log(fileList)
        },
        shangchuan(){
            
            let fd = new FormData();
            fd.append("view", "用户管理"); 
            fd.append("code", "wzwh");
            this.fileList.forEach(item=>{
                //文件信息中raw才是真的文件
                fd.append("up_files",item.raw);
                console.log(fd.get('files'))
                console.log(fd.get("view"))
                console.log(fd.get("code"))
            })
            this.$http.post('/page/set',fd).then(res=>{
              console.log(res);
                // if (res.data.code === 200) {
                //     //console.log(res);
                //     this.$message('上传成功')
                // }else{
                //     this.$message('失败')
                // }
            })
        },
  }
}
</script>

<style scoped lang='scss'>
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
